package mi.payment.ui.print.helper;

import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import mi.payment.R;
import mi.payment.controller.BaseController;
import mi.payment.db.dao.PayDetail;
import mi.payment.ui.base.ParentActivity;
import mi.payment.utils.MoneyUtils;
import mi.payment.utils.CodeUtil;
import mi.payment.utils.SystemDateTime;
import mi.payment.utils.print.DataPrinterKernel;

/**
 * 二维码打印控制器
 */
public class CodeViewController extends BaseController {

    private View mRootView;
    private ImageView mIvCode;
    private ImageView mIvLogo;
    private TextView mTvMerchantName;
    private TextView mTvMerchantNumber;
    private TextView mTvTerminalNumber;
    private TextView mTvType;
    private TextView mTvReferenceNumber;
    private TextView mTvVoucherNumber;
    private TextView mTvOrderNumber;
    private TextView mTvDate;
    private TextView mTvMoney;
    private TextView mTvDescribe;
    private TextView mTvInfo;

    public CodeViewController(ParentActivity activity) {
        super(activity);
    }

    public View getPrintView(PayDetail detail) {
        initView();
        fillData(detail);
        return mRootView;
    }

    private void initView() {
        mRootView = View.inflate(mContext, R.layout.view_qr_print, null);
        mIvLogo = mRootView.findViewById(R.id.iv_pay_logo);
        mTvMerchantName = mRootView.findViewById(R.id.tv_merchant_name);
        mTvMerchantNumber = mRootView.findViewById(R.id.tv_merchant_number);
        mTvTerminalNumber = mRootView.findViewById(R.id.tv_terminal_number);
        mTvType = mRootView.findViewById(R.id.tv_type);
        mTvVoucherNumber = mRootView.findViewById(R.id.tv_voucher_number);
        mTvReferenceNumber = mRootView.findViewById(R.id.tv_reference_number);
        mTvOrderNumber = mRootView.findViewById(R.id.tv_order_number);
        mTvDate = mRootView.findViewById(R.id.tv_date);
        mTvMoney = mRootView.findViewById(R.id.tv_money);
        mIvCode = mRootView.findViewById(R.id.iv_code);
        mTvDescribe = mRootView.findViewById(R.id.tv_describe);
        mTvInfo = mRootView.findViewById(R.id.tv_info);
    }

    private void fillData(PayDetail payDetail) {
        mIvLogo.setImageResource(R.drawable.print_thrid);

        // 打印商户名称
        String merchantName = TextUtils.isEmpty(payDetail.tempMerchantName) ? payDetail.merchantName : payDetail.tempMerchantName;
        merchantName = TextUtils.isEmpty(merchantName) ? "--" : merchantName;
        String temp = getString(R.string.receipt_merchant_name) + merchantName;
        mTvMerchantName.setText(temp);

        // 打印商户号
        String merchantNo = TextUtils.isEmpty(payDetail.tempMerchantNo) ? payDetail.merchantNo : payDetail.tempMerchantNo;
        merchantNo = TextUtils.isEmpty(merchantNo) ? "--" : merchantNo;
        temp = getString(R.string.receipt_merchant_number) + merchantNo;
        mTvMerchantNumber.setText(temp);

        // 打印终端号
        String terminalNo = TextUtils.isEmpty(payDetail.tempTerminalNo) ? payDetail.terminalNo : payDetail.tempTerminalNo;
        terminalNo = TextUtils.isEmpty(terminalNo) ? "--" : terminalNo;
        temp = getString(R.string.receipt_terminal_number) + terminalNo;
        mTvTerminalNumber.setText(temp);

        // 交易类型
        String paymentType;
        if (payDetail.transactionType == 1) {
            paymentType = getString(R.string.consume);
        } else {
            paymentType = getString(R.string.consume_revoke);
        }
        temp = getString(R.string.receipt_type) + paymentType;
        mTvType.setText(temp);

        // 凭证号
        temp = getString(R.string.receipt_voucher_number) + payDetail.voucherNo;
        mTvVoucherNumber.setText(temp);

        // 参考号
        temp = getString(R.string.receipt_reference_number) + payDetail.referNo;
        mTvReferenceNumber.setText(temp);

        // 订单号
        temp = getString(R.string.receipt_order_number) + payDetail.thirdTransNum;
        mTvOrderNumber.setText(temp);

        // 日期
        temp = getString(R.string.receipt_date) + SystemDateTime.getTimeByPayDetails(payDetail);
        mTvDate.setText(temp);

        String amount = MoneyUtils.longCent2DoubleMoneyStr(payDetail.amount);
        if (payDetail.getTransactionType() == 2 || payDetail.getTransactionType() == 3) {
            temp = getString(R.string.receipt_revoke_amount) + "RM " + amount;
        } else {
            temp = getString(R.string.receipt_payment_amount) + "RM " + amount;
        }
        mTvMoney.setText(temp);

        String remark;
        String platform = PrintUtil.getTransactionPlatform(payDetail.transPlatform + "");
        if (payDetail.qrCodeScanModel == 1) {
            remark = getString(R.string.receipt_merchant_scan) + " - " + platform;
        } else {
            remark = getString(R.string.receipt_user_scan) + " - " + platform;
        }
        mTvDescribe.setText(remark);

        if (payDetail.thirdTransNum != null && payDetail.thirdTransNum.length() > 0) {
            mIvCode.setVisibility(View.VISIBLE);
            Bitmap bitmap = CodeUtil.createCodeBitmap(payDetail.thirdTransNum, 150, 150);
            mIvCode.setImageBitmap(bitmap);
        } else {
            mIvCode.setVisibility(View.GONE);
        }

        temp = DataPrinterKernel.INSTANCE().getSmallTicketTail();
        mTvInfo.setText(temp);
    }


}
